--CREATE FOREIGN KEY 1
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_DC_KH ')
BEGIN
	ALTER TABLE DATCHO
	DROP CONSTRAINT FK_DC_KH 
END
GO
ALTER TABLE DATCHO 
ADD CONSTRAINT FK_DC_KH 
FOREIGN KEY (MAKH) 
REFERENCES KHACHHANG (MAKH)
--CREATE FOREIGN KEY 2
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_LB_CB ')
BEGIN
	ALTER TABLE LICHBAY
	DROP CONSTRAINT FK_LB_CB 
END
GO 
ALTER TABLE LICHBAY 
ADD CONSTRAINT FK_LB_CB 
FOREIGN KEY (MACB) 
REFERENCES CHUYENBAY (MACB)
--TAO KHOA NGOAI 1 DATCHO VA LICHBAY
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_DC_LB  ')
BEGIN
	ALTER TABLE DATCHO
	DROP CONSTRAINT FK_DC_LB
END
GO
ALTER TABLE DATCHO 
ADD CONSTRAINT FK_DC_LB 
FOREIGN KEY (NGAYDI,MACB) 
REFERENCES LICHBAY (NGAYDI,MACB)
--TAO KHOA NGOAI 1 MAYBAY VA LOAIMB
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME=' FK_MB_LMB   ')
BEGIN
	ALTER TABLE MAYBAY
	DROP CONSTRAINT FK_MB_LMB
END
GO
ALTER TABLE MAYBAY 
ADD CONSTRAINT FK_MB_LMB 
FOREIGN KEY (MALOAI) 
REFERENCES LOAIMB (MALOAI)
--TAO KHOA NGOAI 1 KHANANG VA LOAIMB
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME=' FK_KN_LMB   ')
BEGIN
	ALTER TABLE KHANANG
	DROP CONSTRAINT FK_KN_LMB
END
GO
ALTER TABLE KHANANG 
ADD CONSTRAINT FK_KN_LMB 
FOREIGN KEY (MALOAI) 
REFERENCES LOAIMB (MALOAI)
--TAO KHOA NGOAI 1 KHANANG VA NHANVIEN
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_KN_NV   ')
BEGIN
	ALTER TABLE KHANANG
	DROP CONSTRAINT FK_KN_NV 
END
GO
ALTER TABLE KHANANG 
ADD CONSTRAINT FK_KN_NV 
FOREIGN KEY (MANV) 
REFERENCES NHANVIEN (MANV)
--TAO KHOA NGOAI 1 PHANCONG VA NHANVIEN
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_PC_NV    ')
BEGIN
	ALTER TABLE PHANCONG
	DROP CONSTRAINT FK_PC_NV 
END
GO
ALTER TABLE PHANCONG 
ADD CONSTRAINT FK_PC_NV 
FOREIGN KEY (MANV) 
REFERENCES NHANVIEN (MANV)
--TAO KHOA NGOAI 1 LICHBAY VA MAYBAY
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_LB_MB  ')
BEGIN
	ALTER TABLE LICHBAY
	DROP CONSTRAINT FK_LB_MB 
END
GO
ALTER TABLE LICHBAY 
ADD CONSTRAINT FK_LB_MB 
FOREIGN KEY (SOHIEU,MALOAI) 
REFERENCES MAYBAY (SOHIEU,MALOAI)

--TAO KHOA NGOAI 1 PHANCONG VA LICHBAY
IF EXISTS (SELECT * FROM SYS.FOREIGN_KEYS WHERE NAME='FK_PC_LB ')
BEGIN
	ALTER TABLE PHANCONG
	DROP CONSTRAINT FK_PC_LB 
END
GO
ALTER TABLE PHANCONG 
ADD CONSTRAINT FK_PC_LB 
FOREIGN KEY (NGAYDI,MACB) 
REFERENCES LICHBAY (NGAYDI,MACB)

